package com.shouer.doc.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.shouer.doc.R;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.ui.main.MainActivity;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by YYY-Work on 2017/9/28.
 */

public class ShouerWebActivity extends AppCompatActivity {

    @BindView(R.id.title)
    protected TextView titleView;
    @BindView(R.id.web_view)
    protected WebView mWebView;

    private String url;
    private String title;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_web_layout);
        getIntentData();
        if (TextUtils.isEmpty(url)) {
            perfectBack();
        }
        ButterKnife.bind(this);
        initViews();
        if (!TextUtils.isEmpty(title)) {
            titleView.setText(title);
        }
        mWebView.loadUrl(url);
    }

    private void getIntentData() {
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");

        if (TextUtils.isEmpty(url)) {
            String extraMap = getIntent().getStringExtra("extraMap");
            if (!TextUtils.isEmpty(extraMap)) {
                try {
                    JSONObject extraObject = new JSONObject(extraMap);
                    if (extraObject.has("url")) {
                        url = extraObject.getString("url");
                    }
                    if (extraObject.has("title")) {
                        title = extraObject.getString("title");
                    }
                } catch (Exception e) {
                    Log.e(ShouerWebActivity.class.getSimpleName(), e.getMessage());
                }

            }
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");

        if (TextUtils.isEmpty(url)) {
            String extraMap = intent.getStringExtra("extraMap");
            if (!TextUtils.isEmpty(extraMap)) {
                try {
                    JSONObject extraObject = new JSONObject(extraMap);
                    if (extraObject.has("url")) {
                        url = extraObject.getString("url");
                    }
                    if (extraObject.has("title")) {
                        title = extraObject.getString("title");
                    }
                } catch (Exception e) {
                    Log.e(ShouerWebActivity.class.getSimpleName(), e.getMessage());
                }

            }
        }

        if (TextUtils.isEmpty(url)) {
            mWebView.loadUrl(url);
        }
    }


    private void initViews() {

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int mDensity = metrics.densityDpi;


        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//关键点
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setLoadWithOverviewMode(true);

        Log.d("maomao", "densityDpi = " + mDensity);
        if (mDensity == 240) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == 160) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        } else if (mDensity == 120) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
        } else if (mDensity == DisplayMetrics.DENSITY_XHIGH) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == DisplayMetrics.DENSITY_XXHIGH) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == DisplayMetrics.DENSITY_XXXHIGH) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else if (mDensity == DisplayMetrics.DENSITY_TV) {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        } else {
            webSettings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        }

        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                /**
                 if (newProgress == 100) {
                 mProgressBar.setVisibility(View.GONE);
                 } else {
                 mProgressBar.setVisibility(View.VISIBLE);
                 mProgressBar.setProgress(newProgress);
                 }*/
            }
        });

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                ShouerLoadingDialogUtil.getInstance().show(ShouerWebActivity.this);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                ShouerLoadingDialogUtil.getInstance().dismiss();
                /**
                 mLoadUrl = url;
                 mGoBackBtn.setEnabled(mWebView.canGoBack());
                 mGoForwardBtn.setEnabled(mWebView.canGoForward());*/
            }

        });

    }

    @OnClick(value = {R.id.back_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                perfectBack();
                break;
        }
    }

    private void perfectBack() {
        if (isTaskRoot()) {
            Intent intent = new Intent(ShouerWebActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            perfectBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
